package edu.gatech.stolen.tea;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import android.util.Log;
/**
 * this class filters the items
 * @author Leshi
 *
 */
public class Filter {
	/**
	 * a simple date formatter
	 */
	static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
	/**
	 * get a list of items that fits in the passed in category criteria
	 * 
	 * @param category
	 * @return a list of item names to display
	 */
	public static ArrayList<String> byCategory(String category, ItemEditor ie) {
		ArrayList<String> categoryItems = new ArrayList<String>();
		for (ToDoItem i : ie.getItemList()) {
			if (i.getCategory().equals(category)) {
				categoryItems.add(i.getName());
			}
		}
		return categoryItems;
	}

	/**
	 * filters the items by completion status
	 * 
	 * @param status
	 * @return a list of item names to display
	 */
	public static ArrayList<String> byCompletion(boolean status, ItemEditor ie) {
		ArrayList<String> statusItems = new ArrayList<String>();
		for (ToDoItem i : ie.getItemList()) {
			if (i.getStatus() == status) {
				statusItems.add(i.getName());
			}
		}
		return statusItems;
	}

	/**
	 * filter items by due date, from closest to farthest
	 * 
	 * @param date
	 * @return a list of item names to display, ordered by closest to due date
	 *         to farthest
	 */
	public static ArrayList<String> byDate(ItemEditor ie) {
		ArrayList<String> nameList = new ArrayList<String>();
		ArrayList<Date> datelist = new ArrayList<Date>();
		for (ToDoItem i : ie.getItemList()) {
			try {
				Date d = sdf.parse(i.getDueDate());
				datelist.add(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		Collections.sort(datelist);
		for (Date da : datelist) {
			for (ToDoItem i : ie.getItemList()) {
				if (i.getDueDate().equals(sdf.format(da))) {
					nameList.add(i.getName());
				}
			}
		}
		return nameList;
	}

	/**
	 * filter by a certain date after
	 * @param date in string
	 * @return
	 */
	public static ArrayList<String> byDueDateAfter(String date, ItemEditor ie) {
		ArrayList<String> nameList = new ArrayList<String>();
		try {
			Date d = sdf.parse(date);
			for (ToDoItem i: ie.getItemList()){
				if(sdf.parse(i.getDueDate()).after(d)){
					nameList.add(i.getName());
				}
			}
		} catch (ParseException e) {
			Log.d("date parse", "unable to parse date "+date);
			e.printStackTrace();
		}
		
		return nameList;
	}

	/**
	 * filter by a certain date before
	 * @param date in string
	 * @return
	 */
	public static ArrayList<String> byDueDateBefore(String date, ItemEditor ie) {
		ArrayList<String> nameList = new ArrayList<String>();
		try {
			Date d = sdf.parse(date);
			for (ToDoItem i: ie.getItemList()){
				if(sdf.parse(i.getDueDate()).before(d)){
					nameList.add(i.getName());
				}
			}
		} catch (ParseException e) {
			Log.d("date parse", "unable to parse date "+date);
			e.printStackTrace();
		}
		
		return nameList;
	}

}
